import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
// 静态加载初始化看到的页面和框架
import Login from '@/views/login'
import Layout from '@/views/layout/Layout.vue'
//隐藏原地跳转错误
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

//配置路由地址 和 页面组件的一一对应关系
const routes = []

// 初始化加载静态路由 所有人都能访问的路由 不需要权限的路由
export const constantRoutes = [
  // 首页重定向
  {
    path: '/',
    redirect: '/home',
    hidden: true,
  },
  // 一级路由 登录
  {
    path: '/login',
    component: Login,
    hidden: true,
  },
  // 首页
  {
    path: '/home',
    component: Layout,
    meta: {path: '/home', title: '后台首页', icon: 'home'},
    children: [
      {
        path: '',
        component: () => import('@/views/home'),
      },
    ],
  },
]

// 初始化加载动态路由 需要划分权限的路由
export const asnycRoutes = [
  /* 订单管理 */
  {
    path: '/order',
    component: Layout,
    meta: {path: '/order', title: '订单管理', roles: ['super'], icon: 'order'},
    children: [
      {
        path: '',
        component: () => import('@/views/order'),
      },
      {
        hidden: true,
        path: '/order/order-detail',
        component: () => import('@/views/order/order-detail.vue'),
      },
      {
        hidden: true,
        path: '/order/order-edit/:id',
        component: () => import('@/views/order/order-edit.vue'),
      },
    ],
  },
  /* 商品管理 */
  {
    path: '/goods',
    component: Layout,
    redirect: '/goods/goods-list',
    meta: {path: '/goods', title: '商品管理', icon: 'goods'},
    children: [
      {
        path: '/goods/goods-list',
        component: () => import('@/views/goods/goods-list'),
        meta: {path: '/goods/goods-list', title: '商品列表'},
      },
      {
        path: '/goods/goods-add',
        component: () => import('@/views/goods/goods-add'),
        meta: {path: '/goods/goods-add', title: '商品添加', roles: ['super']},
      },
      {
        path: '/goods/goods-cate',
        component: () => import('@/views/goods/goods-cate'),
        meta: {path: '/goods/goods-cate', title: '商品分类'},
      },
    ],
  },
  /* 账号管理 */
  {
    path: '/account',
    component: Layout,
    redirect: '/account/account-list',
    meta: {path: '/account', title: '商品分类', icon: 'user'},
    children: [
      {
        path: '/account/account-list',
        component: () => import('@/views/account/account-list'),
        meta: {
          path: '/account/account-list',
          title: '账号列表',
          roles: ['super'],
        },
      },
      {
        path: '/account/account-add',
        component: () => import('@/views/account/account-add'),
        meta: {
          path: '/account/account-add',
          title: '添加账号',
          roles: ['super'],
        },
      },
      {
        path: '/account/account-password',
        component: () => import('@/views/account/account-password'),
        meta: {path: '/account/account-password', title: '修改密码'},
      },
      {
        path: '/account/password-modify',
        component: () => import('@/views/account/password-modify'),
        meta: {path: '/account/password-modify', title: '个人中心'},
      },
    ],
  },
  /* 销售统计 */
  {
    path: '/total',
    component: Layout,
    redirect: '/total/total-goods',
    meta: {path: '/total', title: '销售统计', roles: ['super'], icon: 'total'},
    children: [
      {
        path: '/total/total-goods',
        component: () => import('@/views/total/total-goods'),
        meta: {path: '/total/total-goods', title: '商品统计'},
      },
      {
        path: '/total/total-order',
        component: () => import('@/views/total/total-order'),
        meta: {path: '/total/total-order', title: '订单统计'},
      },
    ],
  },
  /* 店铺管理 */
  {
    path: '/shop',
    component: Layout,
    meta: {path: '/shop', title: '店铺管理', roles: ['super'], icon: 'shop'},
    children: [
      {
        path: '',
        component: () => import('@/views/shop'),
      },
    ],
  },
]

// 路由错误 404相关
export const errorRoutes = [
  // 404
  {
    path: '*',
    redirect: '/404',
  },
  {
    path: '/404',
    component: () => import('@/views/404/error404.vue'),
  },
]

const router = new VueRouter({
  //初始化加载静态路由
  routes: constantRoutes,
})

export default router
